package JZOffer;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class _21 {
    class Solution {
        public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
            ArrayList<Integer> list = new ArrayList<Integer>();
            if (root == null) {
                return list;
            }
            Queue<TreeNode> queue = new LinkedList<TreeNode>();
            queue.offer(root);
            while (!queue.isEmpty()) {
                TreeNode treeNode = queue.poll();
                if (treeNode.left != null) {
                    queue.offer(treeNode.left);
                }
                if (treeNode.right != null) {
                    queue.offer(treeNode.right);
                }
                list.add(treeNode.val);
            }
            return list;
        }
    }

    Solution getSolution() {
        return new Solution();
    }

    public static void main(String args[]) {
        _21 c = new _21();
        _21.Solution s = c.getSolution();
        TreeNode L1 = new TreeNode(3);
        TreeNode L2 = new TreeNode(4);
        TreeNode L3 = new TreeNode(5);
        TreeNode L4 = new TreeNode(6);
        TreeNode L5 = new TreeNode(7);
        L1.left = L2;
        L1.right = L3;
        L2.left = L4;
        L2.right = L5;
        ArrayList<Integer> res = s.PrintFromTopToBottom(L1);
        for (int tem :
                res) {
            System.out.println(tem);
        }


    }
}
